<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\CardSupplement;
use App\Models\Employee;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class CardSupplementController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];


        if ( $request->id) {
            $card_supplement = CardSupplement::findOrFail($request->id);
            $card_supplement->mtime = time();
        } else {
            $card_supplement = new CardSupplement();
            $card_supplement->ctime = time();
        }
        $card_supplement->employee_id = $request->employee_id;
        $card_supplement->company_id = $company_id;
        $card_supplement->shift_id = $request->shift_id;
        $card_supplement->clock_in_ymd = $request->clock_in_ymd;
        $card_supplement->clock_in_time = $request->clock_in_time;
        $card_supplement->card_supplement_date = $request->card_supplement_date;
        $card_supplement->reason = $request->reason;
        $card_supplement->uid = $uid;

        $card_supplement->save();

        return showError(Error::ERR_OK,['id'=>$card_supplement->id]);

    }

    public function show(Request $request) {
        $card_supplement = CardSupplement::findOrFail($request->id);
        return showError(Error::ERR_OK,['card_supplement'=>$card_supplement]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        // 搜索内容
        $where = [];
        $start_date = $request->input('start_date');
        if (!empty($start_date))
        {
            $where['start_date'] = $start_date;
        }
        $end_date = $request->input('end_date');
        if (!empty($end_date))
        {
            $where['end_date'] = $end_date;
        }

        $where['company_id'] = $userInfo['company_id'];
        $where['status'] = 1;

        $list = CardSupplement::getList($where);
        foreach ($list as $k => &$v) {

            $v->employee_info = Employee::where('employee_id',$v->employee_id)->where('company_id',$userInfo['company_id'])->where('status',1)->first();
        }
        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        CardSupplement::where('id',$request->id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
